﻿#pragma once
#ifndef SigImgReal_hpp
#define SigImgReal_hpp

#include<vector>
#include<cstring>
#include<cmath>
#include"../../TesMath/TesLinespace.h"
#include"ImageInterface.hpp"


namespace MMF { namespace Mesh {


//本类中的数据必须是数值型
template<typename T>
class SigImgReal: public ImageInterface
{
    
    int mW;                 ///< 宽
    int mH;                 ///< 高
    float mK;               ///< 信号量化系数
    std::vector<T> mData;   ///< 信号数据

public:

    //构造函数
    SigImgReal(int w, int h, float k, T* data)
        : mW(w)
        , mH(h)
        , mK(k)
    {
        mData.resize(w * h);
        memcpy(mData.data(), data, sizeof(T) * w * h);
    }

    //获取某个坐标的能量值
    float GetValue(int x, int y) override
    {
        return mData[x + (mH - 1 - y) * mW] * mK;
    }

    //获取某个坐标的颜色值
    TesVector3 GetColor(int x, int y) override
    {
        //获得值
        float val = GetValue(x, y);

        //计算dbm
        val = 10 * log10f(1000.0 * val);

        //准备变量
        TesLinespace<float> r;
        TesLinespace<float> g;
        TesLinespace<float> b;
        float low;

        if (val >= -256.0f && val < -128.0f)
        {
            b = TesLinespace<float>(1.0f, 0.2f, 128.0f);
            g = TesLinespace<float>(0.0f, 1.0f, 128.0f);
            r = TesLinespace<float>(0.0f, 0.0f, 128.0f);
            low = -256.0f;
        }
        else if (val >= -128.0f && val < 0.0f)
        {
            b = TesLinespace<float>(0.0f, 0.0f, 128.0f);
            g = TesLinespace<float>(1.0f, 0.5f, 128.0f);
            r = TesLinespace<float>(0.0f, 0.5f, 128.0f);
            low = -128.0f;
        }
        else if (val >= 0.0f && val < 128.0f)
        {
            b = TesLinespace<float>(0.0f, 0.0f, 128.0f);
            g = TesLinespace<float>(0.5f, 0.0f, 128.0f);
            r = TesLinespace<float>(0.5f, 1.0f, 128.0f);
            low = 0.0f;
        }

        TesVector3 ret;
        ret.x = r.GetValue((val - low) / 128.0f);
        ret.y = g.GetValue((val - low) / 128.0f);
        ret.z = b.GetValue((val - low) / 128.0f);

        return ret;
    }

     //尺寸(像素)
    int Width() override
    {
        return mW;
    }
    int Height() override
    {
        return mH;
    }
};


}}




#endif